﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Seagull.BarTender.Print;//需要安装bartender，或者把dll放进项目文件             
using System.Drawing.Printing;

namespace LabelPrint
{
    public partial class WeiWaiForm : Form
    {
        LabelFormatDocument btFormat;
        public WeiWaiForm()
        {
            InitializeComponent();
        }
        private Engine engine = null; // The BarTender Print Engine
        DataSet ds = new DataSet();
        string ITEM_SPECIFICATION = "";
        string UDF022 = "";
        string UDF023 = "";
        double XUDF014 = 0.00;
        double QTY = 0;
        string LOT_CODE = "";
        private void printButton_Click(object sender, EventArgs e)
        {

            SQLServerDataBase Sql = new SQLServerDataBase();
            //68CC257A-21C4-4968-AA1A-18267BA60720
            string selectSqlStr = "SELECT D.ITEM_SPECIFICATION,D.UDF022,D.UDF023,B.XUDF014,B.QTY,E.LOT_CODE FROM [dbo].[WIP_TRANSFER_DOC] A LEFT JOIN WIP_TRANSFER_DOC_D B ON A.WIP_TRANSFER_DOC_ID = B.WIP_TRANSFER_DOC_ID LEFT JOIN MO C ON B.MO_ID = C.MO_ID LEFT JOIN ITEM D ON B.ITEM_ID = D.ITEM_BUSINESS_ID LEFT JOIN ITEM_LOT E ON C.ITEM_LOT_ID = E.ITEM_LOT_ID WHERE C.DOC_NO = '" + textBox1.Text + "' AND B.XUDF014 != 0.000000";

            string bakSelect = "SELECT D.ITEM_SPECIFICATION,D.UDF022,D.UDF023,B.XUDF014,B.QTY,E.LOT_CODE  FROM WIP_TRANSFER_DOC A LEFT JOIN WIP_TRANSFER_DOC_D B ON A.WIP_TRANSFER_DOC_ID = B.WIP_TRANSFER_DOC_ID LEFT JOIN MO C ON B.MO_ID = C.MO_ID LEFT JOIN ITEM D ON B.ITEM_ID = D.ITEM_BUSINESS_ID LEFT JOIN ITEM_LOT E ON C.ITEM_LOT_ID = E.ITEM_LOT_ID LEFT JOIN MO_ROUTING_D F ON B.FROM_MO_ROUTING_D_ID = F.MO_ROUTING_D_ID WHERE C.DOC_NO =  '" + textBox1.Text + "' AND F.OPERATION_ID = '68CC257A-21C4-4968-AA1A-18267BA60720'";//直接从转移单单身中取当前工序ID 
            try
            {
                Sql.jiuwei_ExecuteNonDataSet(bakSelect, out ds);
                DataTable dt = ds.Tables[0];
                ITEM_SPECIFICATION = dt.Rows[0][0].ToString();
                UDF022 = dt.Rows[0][1].ToString();
                UDF023 = dt.Rows[0][2].ToString();
                XUDF014 = Convert.ToDouble(dt.Rows[0][3]);//重量
                QTY = Convert.ToDouble(dt.Rows[0][4]);//数量
                LOT_CODE = dt.Rows[0][5].ToString();                                     // convertZDZSQTY(QTY, ITEM_SPECIFICATION);
                double qty = convertZDZSQTY(QTY, ITEM_SPECIFICATION);
               
                writeTxt("config.txt", ITEM_SPECIFICATION, UDF022, UDF023, XUDF014,Convert.ToDouble(qty.ToString("f2")), LOT_CODE, textBox1.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("重量数据不存在或者为空" + ex.Message);

                return;
            }


         

            printBtw();

          


            label1.Text = ITEM_SPECIFICATION;
            label2.Text = UDF022;
            label3.Text = UDF023;
            label4.Text = XUDF014.ToString();
            label5.Text = QTY.ToString();
            label6.Text = LOT_CODE;

        }


        private int convertDDQty(int qty, string spec)
        {//堆叠换算关系
            switch (spec)
            {
                case "0201":
                    qty *= 114;
                    break;
                case "0402":
                    qty *= 70;
                    break;
                case "0603":
                    qty *= 44;
                    break;
                case "0805":
                    qty *= 34;
                    break;
                case "1206":
                    qty *= 21;
                    break;
                case "1210":
                    qty *= 22;
                    break;
                case "2010":
                    qty *= 13;
                    break;
                case "2512":
                    qty *= 10;
                    break;
            }

            return qty;
        }
        private double convertZDZSQTY(double qty, string spec)
        {
            switch (spec)
            {
                case "0201":
                    qty /= 5.555555;
                    break;
                case "0402":
                    qty /= 8.849557;
                    break;
                case "0603":
                    qty /= 14.285714;
                    break;
                case "0805":
                    qty /= 21.276595;
                    break;
                case "1206":
                    qty /= 26.315789;
                    break;
                case "1210":
                    qty /= 43.47826;
                    break;
                case "2010":
                    qty /= 41.666667;
                    break;
                case "2512":
                    qty /= 55.555556;
                    break;
            }

            return qty;
        }
        private void printBtw()
        {
            lock (engine)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                string filePath = path + "电镀委外物料标识卡" + ".btw";
                btFormat = engine.Documents.Open(filePath);
                //如何传参 命名具名数据源 赋予对应的 key<>value
                //          btFormat.SubStrings["ProductName"].Value = “test1”;
                //传入打印机名称
                btFormat.PrintSetup.PrinterName = cboPrinters.SelectedItem.ToString();
                //打印张数
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;


                Messages messages;
                string appName = "下标打印任务";
                Result result = btFormat.Print(appName, 10000, out messages);
                string messageString = "\n\nMessages:";

                //foreach (Seagull.BarTender.Print.Message message in messages)
                //{
                //    messageString += "\n\n" + message.Text;
                //}

            //    if (result == Result.Failure)
            //        MessageBox.Show(this, "Print Failed" + messageString, appName);
            //    else
            //        MessageBox.Show(this, "文本已成功发送到打印机" + messageString, appName);
            }
        }





        private void writeTxt(string textName,string a,string b,string c,double d, double e,string f,string g)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
          
            string serialNumber = "UDF021|UDF022|UDF023|XUDF014|QTY|LOT_CODE|DOC_NO\r\n" + a + "|" + b + "|" + c + "|" + d + "|" + e + "|" + f + "|" + g;


            using (StreamWriter stream = new StreamWriter(path + "\\" + textName))
            {
                stream.Write(serialNumber);
            }

        }
        string DefaultPrinter1;
        private static PrintDocument fPrintDocument = new PrintDocument();
      //  获取本机默认打印机名称
        public static String DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }
        private void WeiWaiForm_Load(object sender, EventArgs e)
        {
            try
            {
                engine = new Engine(true);
            }
            catch (PrintEngineException exception)
            {
                // If the engine is unable to start, a PrintEngineException will be thrown.
                MessageBox.Show(this, exception.Message);
                this.Close(); // Close this app. We cannot run without connection to an engine.
                return;
            }
            // 得到打印机列表
            Printers printers = new Printers();
            foreach (Printer printer in printers)
            {
                cboPrinters.Items.Add(printer.PrinterName);
            }

            // cboPrinters.SelectedItem = btFormat.PrintSetup.PrinterName;
            cboPrinters.SelectedItem = DefaultPrinter();
            DefaultPrinter1 = DefaultPrinter();
            //  textBox3.Text = DefaultPrinter();
        }

    }
}
